package com.healthyeating.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.healthyeating.common.annotation.Log;
import com.healthyeating.common.core.controller.BaseController;
import com.healthyeating.common.core.domain.AjaxResult;
import com.healthyeating.common.enums.BusinessType;
import com.healthyeating.manage.domain.Eat;
import com.healthyeating.manage.service.IEatService;
import com.healthyeating.common.utils.poi.ExcelUtil;
import com.healthyeating.common.core.page.TableDataInfo;

/**
 * 进食记录Controller
 * 
 * @author 段奇乐
 * @date 2024-12-18
 */
@RestController
@RequestMapping("/manage/eat")
public class EatController extends BaseController
{
    @Autowired
    private IEatService eatService;

    /**
     * 查询进食记录列表
     */
    @PreAuthorize("@ss.hasPermi('manage:eat:list')")
    @GetMapping("/list")
    public TableDataInfo list(Eat eat)
    {
        startPage();
        List<Eat> list = eatService.selectEatList(eat);
        return getDataTable(list);
    }

    /**
     * 导出进食记录列表
     */
    @PreAuthorize("@ss.hasPermi('manage:eat:export')")
    @Log(title = "进食记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Eat eat)
    {
        List<Eat> list = eatService.selectEatList(eat);
        ExcelUtil<Eat> util = new ExcelUtil<Eat>(Eat.class);
        util.exportExcel(response, list, "进食记录数据");
    }

    /**
     * 获取进食记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:eat:query')")
    @GetMapping(value = "/{eatId}")
    public AjaxResult getInfo(@PathVariable("eatId") Long eatId)
    {
        return success(eatService.selectEatByEatId(eatId));
    }

    /**
     * 新增进食记录
     */
    @PreAuthorize("@ss.hasPermi('manage:eat:add')")
    @Log(title = "进食记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<Eat> eatList)
    {
        if (eatList == null || eatList.isEmpty()) {
            return error("填写的进食记录为空，请检查后重新填写");
        }
        return toAjax(eatService.insertEat(eatList));
    }

    /**
     * 修改进食记录
     */
    @PreAuthorize("@ss.hasPermi('manage:eat:edit')")
    @Log(title = "进食记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody List<Eat> eatList)
    {
        return toAjax(eatService.updateEat(eatList));
    }

    /**
     * 删除进食记录
     */
    @PreAuthorize("@ss.hasPermi('manage:eat:remove')")
    @Log(title = "进食记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{eatIds}")
    public AjaxResult remove(@PathVariable Long[] eatIds)
    {
        return toAjax(eatService.deleteEatByEatIds(eatIds));
    }
}
